package org.geometerplus.android.fbreader;

import java.util.HashMap;
import java.util.Map;

//import net.youmi.android.AdManager;

import org.geometerplus.android.fbreader.library.SQLiteBooksDatabase;
import org.geometerplus.android.util.BookUtil;
import org.geometerplus.android.util.Constant;
import org.geometerplus.fbreader.fbreader.FBReaderApp;
import org.geometerplus.fbreader.library.Book;
import org.geometerplus.fbreader.library.BooksDatabase;
import org.geometerplus.fbreader.library.FileInfoSet;
import org.geometerplus.fbreader.library.Library;
import org.geometerplus.zlibrary.core.application.ZLApplication;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.image.ZLImage;
import org.geometerplus.zlibrary.core.image.ZLLoadableImage;
import org.zixuanstudio.ebook.R;
import org.geometerplus.zlibrary.text.view.style.ZLTextBaseStyle;
import org.geometerplus.zlibrary.text.view.style.ZLTextStyleCollection;
import org.geometerplus.zlibrary.ui.android.image.ZLAndroidImageData;
import org.geometerplus.zlibrary.ui.android.image.ZLAndroidImageManager;
import org.geometerplus.zlibrary.ui.android.library.ZLAndroidActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

public class ReaderWelcomeActivity extends Activity {

	private static final long WAITS_TIME = 500;
	final ZLTextStyleCollection collection = ZLTextStyleCollection.Instance();
	final ZLTextBaseStyle baseStyle = collection.getBaseStyle();

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			Intent intent = null;
			switch (msg.what) {
			case 0:
				intent = new Intent(ReaderWelcomeActivity.this, BookShelf.class);
				startActivity(intent);
				finish();
				break;
			case 1:
				Cursor cursor = Book.getAllBook();
				String path = null;
				if (cursor != null && cursor.moveToFirst()) {
					path = cursor.getString(5);
				}
				intent = new Intent(ReaderWelcomeActivity.this, FBReader.class);
				if (path != null)
					intent.putExtra(FBReader.BOOK_PATH_KEY, path);

				startActivity(intent);
				finish();
				break;
			}

		}

	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 应用Id 应用密码 广告请求间隔(s) 测试模式
		// AdManager.init(this,"480655328b365dc3", "a32e02720befac54 ", 30,
		// false);
		if (!BookUtil.isCanUseSdcard()) {
			Toast.makeText(this, R.string.sdcard_unavilable, Toast.LENGTH_SHORT)
					.show();
			finish();
			return;
		}

		Thread.setDefaultUncaughtExceptionHandler(new org.geometerplus.zlibrary.ui.android.library.UncaughtExceptionHandler(
				this));
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		if (SQLiteBooksDatabase.Instance() == null) {
			new SQLiteBooksDatabase(this, "READER");

		}

		setContentView(R.layout.welcome);
		if (baseStyle.getLaunchOption()) {
			// ImageUtil.startLoadBookCover(this,mHandler);
			SharedPreferences preference = ReaderWelcomeActivity.this
					.getSharedPreferences(Constant.REAER_PREFERENCE_NAME,
							ReaderWelcomeActivity.this.MODE_PRIVATE);
			boolean isInit = preference.getBoolean(Constant.INIT_BOOKDATABASE,
					false);
			if (!isInit) {
				new Thread(mLoadBookTask).start();
			} else {
				mHandler.sendEmptyMessageDelayed(0, WAITS_TIME);
			}
		} else {
			mHandler.sendEmptyMessageDelayed(1, 100);
		}

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		// mHandler.removeMessages(0);
	}

	private Runnable mLoadBookTask = new Runnable() {

		@Override
		public void run() {
			Looper.prepare();
			initBooksDatabases();
			Looper.loop();
		}

	};

	/**
	 * 在初始化的时候，默认添加一本书
	 */
	private void initBooksDatabases() {
		BookUtil.mInitListener = mInitBooksListener;
		BookUtil.getHelperBook(this);
	}

	private BookUtil.mLoadListener mInitBooksListener = new BookUtil.mLoadListener() {

		@Override
		public void onInitError() {
			Toast.makeText(ReaderWelcomeActivity.this,
					R.string.init_book_error, Toast.LENGTH_SHORT).show();
			mHandler.sendEmptyMessage(0);
		}

		@Override
		public void onInitComplete() {
			SharedPreferences preference = ReaderWelcomeActivity.this
					.getSharedPreferences(Constant.REAER_PREFERENCE_NAME,
							ReaderWelcomeActivity.this.MODE_PRIVATE);
			SharedPreferences.Editor editor = preference.edit();
			editor.putBoolean(Constant.INIT_BOOKDATABASE, true);
			editor.commit();
			// refreshBooksList();
			mHandler.sendEmptyMessage(0);
		}

		@Override
		public void onInit(String bookPath) {
			Book.getHelperBook(bookPath);

		}
	};
}
